/*    */
package com.thtfpc.stroke.screening.util;
/*    */
/*    */

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateConvertUtil {
    public static Logger logger = LoggerFactory.getLogger(DateConvertUtil.class);
    public static final String defaultFormmtter = "yyyy-MM-dd :HH:mm:ss";
    public static final String dateFormatter = "MMddHHmmss";
    public static final String yearFormatter = "yy";

    public static String covvert2String(Date date, String formatterStr) {
        SimpleDateFormat formatter = new SimpleDateFormat(formatterStr);
        String result = formatter.format(date);
        return result;
    }

    public static int getAge(Date birthDay) {
        Calendar cal = Calendar.getInstance();
        if (cal.before(birthDay)) {
            throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
        }

        int yearNow = cal.get(1);
        int monthNow = cal.get(2);
        int dayOfMonthNow = cal.get(5);
        cal.setTime(birthDay);

        int yearBirth = cal.get(1);
        int monthBirth = cal.get(2);
        int dayOfMonthBirth = cal.get(5);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) age--;
            } else {
                age--;
            }
        }
        return age;
    }

    public static String getCurrentYear() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = new Date();
        return sdf.format(date);
    }


    public static void main(String[] args) {
        System.out.println(covvert2String(new Date(), "yyyy"));
    }

}


